package com.joe.listener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.ContextLoaderListener;

import javax.servlet.ServletContextEvent;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class DemoListener extends ContextLoaderListener {
    private static Logger logger = LoggerFactory.getLogger(DemoListener.class);
    //private WebApplicationContext context;

    @Override
    public void contextInitialized(ServletContextEvent event) {
        //context = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
        serverInitialized();
    }

    private void serverInitialized() {

        ExecutorService es = Executors.newCachedThreadPool();
        es.execute(new DemoWorker());

    }

    private class DemoWorker implements Runnable {

        @Override
        public void run() {
            while (true) {
                logger.info(" Test demo ");
                try {
                    TimeUnit.MINUTES.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
